package com.wangdao.wechatmall.controller.wx;

import com.wangdao.wechatmall.bean.bo.wx.*;
import com.wangdao.wechatmall.bean.pojo.Comment;
import com.wangdao.wechatmall.bean.pojo.OrderGoods;
import com.wangdao.wechatmall.bean.vo.admin.BaseRespVo;
import com.wangdao.wechatmall.bean.vo.wx.WXOrderDetailVO;
import com.wangdao.wechatmall.bean.vo.wx.WXOrderListVO;
import com.wangdao.wechatmall.service.wx.WXOrderService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author liyongqiang
 * CreateDate 2021/7/11 17:17
 */

@RestController
@RequestMapping("wx/order")
public class WXOrderController {

    @Autowired
    WXOrderService orderService;

    @RequestMapping("list")
    public BaseRespVo queryList(WXOrderBO orderBO) {

        WXOrderListVO orderListVO = orderService.queryList(orderBO);
        return BaseRespVo.ok(orderListVO);
    }

    @RequestMapping("detail")
    public BaseRespVo orderDetail(Integer orderId) {
        WXOrderDetailVO orderDetailVO = orderService.getDetail(orderId);
        return BaseRespVo.ok(orderDetailVO);
    }

    @RequestMapping("prepay")
    public BaseRespVo orderPrepay(@RequestBody WXOrderIdBO prepayBO) {
        int id = 0;
        if (prepayBO.getOrderId() != null){
            id = Integer.parseInt(prepayBO.getOrderId());
        }else {
            id = (int) SecurityUtils.getSubject().getSession().getAttribute("submitOrderId");
        }
        int prepay = orderService.prepay(id);
        if (prepay == 1) {
            return BaseRespVo.ok("付款成功");
        }
        return BaseRespVo.fail("付款失败");
    }

    @RequestMapping("cancel")
    public BaseRespVo orderCancel(@RequestBody WXOrderIdBO WXOrderIdBO) {
        int id = Integer.parseInt(WXOrderIdBO.getOrderId());
        int cancel = orderService.cancel(id);
        if (cancel == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("delete")
    public BaseRespVo orderDelete(@RequestBody WXOrderIdBO WXOrderIdBO) {
        int id = Integer.parseInt(WXOrderIdBO.getOrderId());
        int delete = orderService.delete(id);
        if (delete == 1) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("confirm")
    public BaseRespVo orderConfirm(@RequestBody WXOrderIdBO WXOrderIdBO){
        int id = Integer.parseInt(WXOrderIdBO.getOrderId());
        int confirm = orderService.confirm(id);
        if (confirm == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("refund")
    public BaseRespVo orderRefund(@RequestBody WXOrderIdBO WXOrderIdBO){
        int id = Integer.parseInt(WXOrderIdBO.getOrderId());
        int refund = orderService.refund(id);
        if (refund == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("goods")
    public BaseRespVo orderGoods(WXOrderGoodsBO WXOrderGoodsBO){

        OrderGoods orderGoods = orderService.getGoods(WXOrderGoodsBO);
        return BaseRespVo.ok(orderGoods);

    }

    @RequestMapping("comment")
    public BaseRespVo orderComment(@RequestBody WXOrderCommentBO commentBO){
        int comment = orderService.orderComment(commentBO);
        if (comment == 1){
            return BaseRespVo.ok();
        }
        return BaseRespVo.fail();
    }

    @RequestMapping("submit")
    public BaseRespVo orderSubmit(@RequestBody WXOrderSubmitBO submitBO){
        int orderId = orderService.orderSubmit(submitBO);
        SecurityUtils.getSubject().getSession().setAttribute("submitOrderId", orderId);
        return BaseRespVo.ok(orderId);
    }
}


